package design.patterns.mementomode.checkpointmemento;

import java.util.ArrayList;
import java.util.List;

/**
 * @Desc:   负责人角色，负责人可以根据检查点指数 index 来恢复发起人角色的状态
 * @author: cww
 * @DateTime: 2020-06-13 18:25
 */

public class Caretaker {
    private Originator o;
    private List<Memento> mementos = new ArrayList<>();
    private int current;

    public Caretaker(Originator o) {
        this.o = o;
        current = 0;
    }

    /**
     * 创建一个新检查点
     * @return
     */
    public int createMemento() {
        Memento m = o.createMemento();
        mementos.add(m);
        return current++;
    }

    /**
     * 将发起人恢复到某个检查点
     */
    public void restoreMemento(int index){
        Memento memento = mementos.get(index);
        o.restoreMemento(memento);
    }

    /**
     * 将某个检查点删除
     */

    public void removeMemento(int index){
        mementos.remove(index);
    }
}
